package sz.szsmk.citizencard.ui.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chenenyu.router.annotation.Route;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.SubwaySiteDetailActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryMetroExitInfoRes;
import sz.szsmk.citizencard.mvp.contract.ISubwaySiteDetailContact;
import sz.szsmk.citizencard.mvp.presenter.SubwaySiteDetailPresenter;
import sz.szsmk.citizencard.ui.adapter.SubwaySiteAdapter;
import sz.szsmk.citizencard.ui.adapter.SubwaySiteInfoAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.MesageEventBus;
import sz.szsmk.citizencard.util.Utils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/10 16:18
 * description:
 */
@Route("SubwaySiteDetailActivity")
public class SubwaySiteDetailActivity extends IBaseActivity<SubwaySiteDetailPresenter, SubwaySiteDetailActivityBinding>
        implements ISubwaySiteDetailContact.ISubwaySiteDetailView, View.OnClickListener {

    ImageView back;
    TextView settingTitle;
    RecyclerView subwaySiteRv;
    RecyclerView subwaySitInfo;
    TextView setStart;
    TextView setEnd;
    LinearLayout setLayout;
    private List<QueryMetroExitInfoRes.MetroExtraInfoListBean> metroExtraInfoList = new ArrayList<>();
    private SubwaySiteAdapter subwaySiteAdapter;
    private SubwaySiteInfoAdapter subwaySiteInfoAdapter;
    private String adress;
    private double longitude;
    private double latitude;
    private String line_name;
    private String stand_name;
    private String fromActivity;
    @Override
    protected int attachLayoutRes() {
        return R.layout.subway_site_detail_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {

        back = findViewById(R.id.back);
        settingTitle = findViewById(R.id.setting_title);
        subwaySiteRv = findViewById(R.id.subway_site_rv);
        subwaySitInfo = findViewById(R.id.subway_site_info);
        setStart = findViewById(R.id.set_start);
        setStart.setOnClickListener(this);
        setEnd = findViewById(R.id.set_end);
        setEnd.setOnClickListener(this);
        setLayout  = findViewById(R.id.set_layout);


//        showLoadingDialog();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adress = getIntent().getStringExtra("type");
        try{
            fromActivity=getIntent().getStringExtra("from");
        }catch (Exception e){

        }

        mPresenter.getSiteDetailData(adress);
        dataBinding.subwaySiteDetailGomap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.startMapBylocationByBaiduCorSys(SubwaySiteDetailActivity.this, latitude, longitude, stand_name);
            }
        });
        settingTitle.setText("站点详情");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        if("tripPlan".equals(fromActivity)){
            setLayout.setVisibility(View.GONE);
        }

        subwaySiteRv.setLayoutManager(linearLayoutManager);
        subwaySiteAdapter = new SubwaySiteAdapter(this);
        subwaySiteRv.setNestedScrollingEnabled(false);
        subwaySiteRv.setAdapter(subwaySiteAdapter);

        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);


        subwaySitInfo.setLayoutManager(linearLayoutManager1);
        subwaySiteInfoAdapter = new SubwaySiteInfoAdapter(this);
        subwaySitInfo.setNestedScrollingEnabled(false);
        subwaySitInfo.setAdapter(subwaySiteInfoAdapter);
    }

    @Override
    protected void updateViews() {
    }

    @Override
    public void getSiteDetailDataSucc(QueryMetroExitInfoRes response) {

        metroExtraInfoList = response.getMetroExtraInfoList();

        longitude = metroExtraInfoList.get(0).getLongitude();
        latitude = metroExtraInfoList.get(0).getLatitude();
        stand_name = metroExtraInfoList.get(0).getStand_name();
        dataBinding.subwaySiteDetailName.setText(metroExtraInfoList.get(0).getStand_name());
        dataBinding.subwaySiteDetailAdress.setText(metroExtraInfoList.get(0).getStand_address());
        List<QueryMetroExitInfoRes.MetroExtraInfoListBean.MetroLineListBean> metroLineList = metroExtraInfoList.get(0).getMetroLineList();
        subwaySiteInfoAdapter.setViewData(metroLineList);
        subwaySiteInfoAdapter.notifyDataSetChanged();

        List<QueryMetroExitInfoRes.MetroExtraInfoListBean.StandExtraListBean> standExtraList = metroExtraInfoList.get(0).getStandExtraList();
        subwaySiteAdapter.setViewData(standExtraList);
        subwaySiteAdapter.notifyDataSetChanged();

    }

    @Override
    public void getSiteDetailDataFail() {

    }

    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.set_start) {//                Toast.makeText(this, "设置起点", Toast.LENGTH_SHORT).show();
            MesageEventBus mesageEventBus = new MesageEventBus();
            mesageEventBus.setStart(adress);
            mesageEventBus.setLat(latitude);
            mesageEventBus.setLog(longitude);
            EventBus.getDefault().postSticky(mesageEventBus);
            finish();
//                EventBus.getDefault().post(new MessageEvent(MessageEvent.TO_SETSTART));
        } else if (id == R.id.set_end) {
            MesageEventBus mesageEvent = new MesageEventBus();
            mesageEvent.setEnd(adress);
            mesageEvent.setLat(latitude);
            mesageEvent.setLog(longitude);
            EventBus.getDefault().postSticky(mesageEvent);
            finish();
//                Toast.makeText(this, "设置终点", Toast.LENGTH_SHORT).show();
//                EventBus.getDefault().post(new MessageEvent(MessageEvent.TO_SETEND));
        }
    }


    @Override
    public void onClick(View v) {
        onViewClicked(v);
    }
}
